<template>
	<view class="page">
		<view class="consultation-detail-container">
			<u-gap height="32rpx" bgColor="transparent"></u-gap>
			<view class="form-card-title">患者信息</view>
			<view class="card-container">
				<u-cell-group :border="false">
					<u-cell title="姓名" :value="detail.consult.memberName"></u-cell>
					<u-cell title="性别" :value="detail.consult.gender==1?'男':'女'"></u-cell>
					<u-cell title="年龄" :value="detail.consult.memberAge"></u-cell>
					<u-cell title="出生日期" :value="detail.consult.birth?detail.consult.birth.split(' ')[0]:''"></u-cell>
					<u-cell title="身份证号" :value="detail.consult.idcard"></u-cell>
				</u-cell-group>
				<!-- <u--textarea v-model="detail.consult.mainsuit" border="none" placeholder="请输入内容"></u--textarea> -->
			</view>
			<u-gap height="32rpx" bgColor="transparent"></u-gap>
			<view class="form-card-title">病例信息</view>
			<view class="card-container">
				<u-cell-group :border="false">
					<u-cell title="主诉" :value="detail.consult.mainsuit"></u-cell>
					<u-cell title="临床诊断" :value="detail.consult.assessmentcontent"></u-cell>
					<!-- <u-cell title="病例资料" :value="新版本"></u-cell> -->
					<!-- <u-cell title="影响资料" :value="新版本" :border="false"></u-cell> -->
				</u-cell-group>
			</view>
			<u-gap height="32rpx" bgColor="transparent"></u-gap>
			<view class="form-card-title">会诊信息</view>
			<view class="card-container">
				<u-cell-group :border="false">
					<u-cell title="会诊目的" :value="detail.consult.conGoal" />
					<u-cell title="会诊医院" :value="detail.slave.hospitalName" />
					<u-cell title="会诊科室" :value="detail.slave.deptName" />
					<u-cell title="会诊医生" :value="detail.slave.doctorName" />
					<u-cell title="会诊时间" :value="detail.consult.conTime" :border="false" />
				</u-cell-group>
			</view>

			<u-gap height="32rpx" bgColor="transparent"></u-gap>
			<u--form labelPosition="left" :model="model1" ref="uForm">
				<view class="card-container">
					<u-form-item label="病情照片" :labelWidth='90' style="margin-left: 20rpx;">
						<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="10"></u-upload>
					<!-- 	<u--image style="margin-left: 10rpx;" v-for="item in fileList1" :showLoading="true"
							:src="item.url" width="80px" height="80px"></u--image> -->
					</u-form-item>
				</view>
			</u--form>
			<!-- 会诊报告弹窗 -->
			<u-popup :show="showReportPopup" @close="closeReport" mode="center" round="10" :closeable="true">
				<view class="report-popup">
					<view class="report-header">
						<view class="report-title">朝阳县中心医院紧密型医共体</view>
						<view class="report-subtitle">远程会诊报告</view>
						<view class="report-no">业务编号：{{detail.consult.applyCode}}</view>
					</view>

					<view class="report-content">
						<view class="report-row">
							<view class="report-col">
								<text class="label">患者姓名：</text>
								<text class="value">{{detail.consult.memberName}}</text>
							</view>
							<view class="report-col">
								<text class="label">患者性别：</text>
								<text class="value">{{ detail.consult.gender == 1 ? '男' : '女'}}</text>
							</view>
							<view class="report-col">
								<text class="label">患者年龄：</text>
								<text class="value">{{ detail.consult.memberAge}}</text>
							</view>
						</view>

						<view class="report-row">
							<view class="report-col">
								<text class="label">出生日期：</text>
								<text
									class="value">{{ detail.consult.birth ? detail.consult.birth.split(' ')[0] : '--' }}</text>
							</view>
							<view class="report-col">
								<text class="label">证件类型：</text>
								<text
									class="value">{{ detail.consult.idcardType == 1 ? '二代身份证' : detail.consult.idcardType == 2 ? '军官证/士兵证' :detail.consult.idcardType == 3? '护照' : detail.consult.idcardType == 4 ? '其他' : detail.consult.idcardType == 5 ? '台湾居民来往大陆通行证' :'港澳居民来往内地通行证' }}</text>
							</view>
						</view>

						<view class="report-row">
							<view class="report-col">
								<text class="label">证件号码：</text>
								<text class="value">{{ detail.consult.idcard }}</text>
							</view>
							<view class="report-col">
								<text class="label">保险类型：</text>
								<text
									class="value">{{ detail.consult.insuranceType == 1 ? '城镇职工基本医疗保险' : detail.consult.insuranceType == 2 ? '城镇居民基本医疗保险' : detail.consult.insuranceType == 3 ? '新型农村合作医疗' : detail.consult.insuranceType == 4 ? '贫困救助' : detail.consult.insuranceType == 5 ? '商业医疗保险' : detail.consult.insuranceType == 6 ? '全公费' : detail.consult.insuranceType == 7 ? '全自费' : detail.consult.insuranceType == 8 ? '其他' : '不详' }}</text>
							</view>
						</view>

						<view class="report-row">
							<view class="report-col">
								<text class="label">申请机构：</text>
								<text class="value">{{detail.consult.applyHospitalName}}</text>
							</view>
							<view class="report-col">
								<text class="label">申请科室：</text>
								<text class="value">{{detail.consult.applyDeptName}}</text>
							</view>
						</view>

						<view class="report-row">
							<view class="report-col">
								<text class="label">申请医生：</text>
								<text class="value">{{detail.consult.applyDoctorName}}</text>
							</view>
							<view class="report-col">
								<text class="label">会诊类型：</text>
								<text class="value">{{ detail.consult.conMold == 1 ? '临床会诊' : detail.consult.conMold == 2 ? '影像诊断' :
              detail.consult.conMold == 3 ?
                '心电诊断' : detail.consult.conMold == 4 ? '病理诊断' : detail.consult.conMold == 5 ? '远程示教' :
                  detail.consult.conMold == 6 ? '精准医疗' :
                    detail.consult.conMold == 7 ?
                      '卒中急救' : detail.consult.conMold == 8 ? '病理讨论' : detail.consult.conMold == 9 ? '护理会诊' :
                        detail.consult.conMold == 10
              ? '远程门诊' : '检验诊断' }}</text>
							</view>
						</view>

						<view class="report-row">
							<view class="report-col">
								<text class="label">会诊方式：</text>
								<text class="value">{{ detail.consult.conWay == 0 ? '视频' : '非视频' }}</text>
							</view>
							<view class="report-col">
								<text class="label">会诊模式：</text>
								<text class="value">{{ !detail.consult.conMode && detail.consult.conMode != 0 ? '未知' : detail.consult.conMode == 0 ?
              '单方' : '多方' }}</text>
							</view>
						</view>

						<view class="report-section">
							<view class="section-item">
								<text class="label">会诊目的：</text>
								<text class="value">{{detail.consult.conGoal || '--'}}</text>
							</view>

							<view class="section-item">
								<text class="label">ICD-10诊断：</text>
								<text class="value">{{detail.slave.diseaseName || '--'}}</text>
							</view>

							<view class="section-item">
								<text class="label">诊断描述：</text>
								<text class="value">{{detail.slave.assessmentDesc || '--'}}</text>
							</view>

							<view class="section-item">
								<text class="label">治疗意见：</text>
								<text class="value">{{detail.slave.handlingOpinions || '--'}}</text>
							</view>
						</view>

						<view class="report-footer">
							<view class="doctor-info">
								<view>
									<text>报告医生：{{detail.slave.doctorName}}</text>
									<text style="margin-left: 20rpx;">报告时间：{{detail.slave.createTime}}</text>
								</view>
								<view>
									<text>接诊机构：{{detail.slave.hospitalName}}</text>

								</view>
								<view>
									<text>接诊科室：{{detail.slave.deptName}}</text>
									<text style="margin-left: 40rpx;">接诊医生（主持）：{{detail.slave.doctorName}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
		<view class="button-group-list">
			<view class="save-button">
				<u-button v-if="detail.consult.conStatus===9" type="primary" text="查看会诊报告" @click="showReport"
					shape="circle" />
			</view>
			<view class="save-entry-button">
				<u-button type="warning" text="呼叫会诊" shape="circle" @click="onCalling"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import config from '@/common/config'
	import {
		v1EhrConsultationDetail
	} from '@/common/api/consultation/detail/detail.js'

	const app = getApp()

	export default {
		onLoad(query) {
			this.query.id = query.id
			this.getDetail()
		},
		data() {
			return {
				query: {
					id: ''
				},
				model1: {},
				fileList1: [],
				detail: {
					consult: {},
					slave: {}
				},
				formRules: {},
				showReportPopup: false,
				conStatus: null
			}
		},
		methods: {
			getDetail() {
				v1EhrConsultationDetail(this.query.id).then((response) => {
					this.detail = response.object;
					if (response.object && response.object.consult) {
						// 按逗号分割字符串为数组
						const parts = response.object.consult.caseImages.split(',');
						// 将数组中的每个元素转换为对象
						const result = parts.map(item => ({
							url: config.imgURl + item
						}));
						result.forEach((item) => {
							this.fileList1.push(item);
						});
					} else {
						this.fileList1 = [];
					}
				})
			},
			// 呼叫会诊
			onCalling() {
				uni.navigateTo({
					url: '/pages/consultation/meeting/meeting?businessId=' + this.query.id
				})
			},
			showReport() {
				this.showReportPopup = true;
			},
			closeReport() {
				this.showReportPopup = false;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 0 0 calc(64rpx + 120rpx + env(safe-area-inset-bottom)) 0;
		box-sizing: border-box;
	}

	.form-card-title {
		padding: 0 42rpx 16rpx;
		font-size: 24rpx;
		color: $text-desc-color;
	}

	.card-container {
		padding: 0 20rpx;
	}

	.button-group-list {
		position: fixed;
		left: 32rpx;
		bottom: calc(32rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0 10rpx;
		width: 686rpx;
		height: 100rpx;
		border-radius: 1000rpx;
		background-color: #fff;
		box-shadow: 0 0 15rpx 0 rgba(0, 0, 0, 0.05);
		z-index: 10;

		.save-button {
			width: 266rpx;
		}

		.save-entry-button {
			width: 266rpx;
		}
	}

	.report-button {
		margin-top: 16rpx;
		padding: 0 20rpx;
	}

	.report-popup {
		width: 95vw;
		max-height: 85vh;
		overflow-y: auto;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 50rpx;

		.report-header {
			border-bottom: 2rpx solid #eee;
			padding-bottom: 40rpx;
			margin-bottom: 40rpx;

			.report-title {
				text-align: center;
				font-size: 32rpx;
				font-weight: bold;
				margin-bottom: 24rpx;
				color: #333;
				letter-spacing: 2rpx;
			}

			.report-subtitle {
				text-align: center;
				font-size: 28rpx;
				margin-bottom: 36rpx;
				color: #333;
				letter-spacing: 2rpx;
			}

			.report-no {
				text-align: right;
				font-size: 28rpx;
				color: #666;
				padding-right: 10rpx;
			}
		}

		.report-content {
			padding: 0 20rpx;

			.report-row {
				display: flex;
				flex-wrap: nowrap;
				border-bottom: 2rpx solid #eee;
				padding: 20rpx 0;
				margin: 0;

				.report-col {
					flex: 1;
					display: flex;
					align-items: center;
					padding: 0 20rpx;
					position: relative;

					&:not(:last-child):after {
						display: none;
					}

					.label {
						color: #666;
						font-size: 24rpx;
						white-space: nowrap;
						min-width: 120rpx;
					}

					.value {
						color: #333;
						font-size: 24rpx;
						margin-left: 8rpx;
						flex: 1;
					}
				}

				// 第一行的特殊处理（姓名和性别）
				&:first-child {
					border-bottom: none;

					.report-col {
						justify-content: flex-start;

						&:first-child {
							flex: 3;
						}

						&:nth-child(2) {
							flex: 0.5;
						}

						&:last-child {
							flex: 0.5;
						}
					}
				}

				// 出生日期和证件类型在同一行
				&:nth-child(2) {
					border-bottom: none;

					.report-col {
						&:first-child {
							flex: 1;
						}

						&:last-child {
							flex: 1;
							padding-left: 20rpx;
						}
					}
				}

				// 证件号码和保险类型行的特殊处理
				&:nth-child(4) {
					border-bottom: none;

					.report-col {
						&:first-child {
							flex: 1; // 证件号码占更多空间
						}

						&:last-child {
							flex: 1; // 保险类型占较少空间
						}
					}
				}

				// 去掉申请机构和申请科室行的下方横线
				&:nth-child(5) {
					border-bottom: none;

					.report-col {
						&:first-child {
							flex: 1;
						}

						&:last-child {
							flex: 1;
						}
					}
				}

				// 调整会诊信息行的布局
				&:nth-child(6) {
					.report-col {
						&:first-child {
							flex: 1;
						}

						&:nth-child(2) {
							flex: 1;
						}

						&:last-child {
							flex: 1;
						}
					}
				}
			}

			.report-section {
				margin-top: 40rpx;
				border-top: 2rpx solid #eee;
				padding-top: 30rpx;

				.section-item {
					margin-bottom: 32rpx;
					background-color: #f8f8f8;
					padding: 20rpx;
					border-radius: 12rpx;

					.label {
						display: block;
						color: #666;
						font-size: 24rpx;
						margin-bottom: 12rpx;
						font-weight: 500;
					}

					.value {
						display: block;
						color: #333;
						font-size: 24rpx;
						line-height: 1.8;
						padding: 16rpx 24rpx;
						background: #fff;
						border-radius: 8rpx;
					}
				}
			}

			.report-footer {
				margin-top: 50rpx;
				padding-top: 30rpx;
				border-top: 2rpx solid #eee;

				.doctor-info {
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					gap: 30rpx;
					font-size: 24rpx;
					color: #666;
					padding-right: 20rpx;
				}
			}
		}
	}

	// .u-popup {
	// 	.u-popup-close {
	// 		right: 10rpx;
	// 	}
	// }
</style>